package model;

import java.sql.*;
import java.util.*;


public class QnaCommentDAO {

	public static void main(String[] args){
		QnaCommentDAO qnaDAO = new QnaCommentDAO();
		QnaComment qc = new QnaComment();
		qc.setQcno(1);
		qc.setQbno(3);
		qc.setQccomment("ffffffff");
		
//		System.out.println(qnaDAO.insert(qc));
//		System.out.println(qnaDAO.updateComment(qc));
//		System.out.println(qnaDAO.delete(qc));
//		System.out.println(qnaDAO.getQnaComment(qc).getQccomment());
//		System.out.println(qnaDAO.getQnaCommentList().elements().nextElement());
//		System.out.println(qnaDAO.getTotalRows(qc));
	}
	
		public boolean insert(QnaComment qc){
			Connection conn= ConnectionManager.getConnection();
			try{
				PreparedStatement pstmt = conn.prepareStatement(
						"insert into qnacomment (qbno, qccomment, qcdate) values(?,?,now())"
				);
				pstmt.setInt(1, qc.getQbno());
				pstmt.setString(2, qc.getQccomment());
				pstmt.executeUpdate();
				conn.close();
				
				return true;
			} catch(Exception e){
				e.printStackTrace();
				return false;
			}finally{
				if(conn!=null) try { conn.close(); } catch(Exception e) {}
			}
		}
		
//		public boolean updateComment(QnaComment qc){
//			Connection conn= ConnectionManager.getConnection();
//			try{
//				PreparedStatement pstmt = conn.prepareStatement(
//						"update qnacomment set qccomment=? where qcno=?"
//				);
//				pstmt.setString(1, qc.getQccomment());
//				pstmt.setInt(2, qc.getQcno());
//				pstmt.executeUpdate();
//				conn.close();
//				
//				return true;
//			} catch(Exception e){
//				e.printStackTrace();
//				return false;
//			}finally{
//				if(conn!=null) try { conn.close(); } catch(Exception e) {}
//			}
//		}
		
		
		public boolean delete(QnaComment qc){
			Connection conn= ConnectionManager.getConnection();
			try{
				PreparedStatement pstmt = conn.prepareStatement(
						"delete from qnacomment where qcno=?"
				);
				pstmt.setInt(1, qc.getQcno());
				pstmt.executeUpdate();
				return true;
			} catch(Exception e){
				e.printStackTrace();
				return false;
			}finally{
				if(conn!=null) try { conn.close(); } catch(Exception e) {}
			}
		}
	
		
		public QnaComment  getQnaComment(QnaComment qc){
			QnaComment qnacomment = null;
			Connection conn = ConnectionManager.getConnection();
			try {
				///////////////////////////////////////////////
				PreparedStatement pstmt = conn.prepareStatement(
							"select *  from qnacomment where qcno=?"
						);
				pstmt.setInt(1, qc.getQcno());
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					qnacomment= new QnaComment();
					qnacomment.setQcno(rs.getInt(1));
					qnacomment.setQbno(rs.getInt(2));
					qnacomment.setQccomment(rs.getString(3));
					qnacomment.setQcdate(rs.getString(4));
				}
				rs.close();
				pstmt.close();
				///////////////////////////////////////////////
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				if(conn!=null) try { conn.close(); } catch(Exception e) {}
			}
			return qnacomment;
		}	
		
		public Vector  getQnaCommentList(){
			Vector vector = new Vector();
			Connection conn = ConnectionManager.getConnection();
			try {
				///////////////////////////////////////////////
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(
						"select *  from qnacomment"
				);
				while(rs.next()) {
					QnaComment qnacomment = new QnaComment();
					qnacomment.setQcno(rs.getInt(1));
					qnacomment.setQbno(rs.getInt(2));
					qnacomment.setQccomment(rs.getString(3));
					qnacomment.setQcdate(rs.getString(4));
					vector.add(qnacomment);
				}
				rs.close();
				stmt.close();
				///////////////////////////////////////////////
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				if(conn!=null) try { conn.close(); } catch(Exception e) {}
			}
			return vector;
		}	
		
		public long getTotalRows(QnaComment qc) {
			long result = 0;
			Connection conn = ConnectionManager.getConnection();
			try {
				///////////////////////////////////////////////
				PreparedStatement pstmt = conn.prepareStatement(
						"select count(*) from qnacomment"
				);
				ResultSet rs = pstmt.executeQuery();
				rs.next();
				result = rs.getLong(1);
				rs.close();
				pstmt.close();
				///////////////////////////////////////////////
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				if(conn!=null) try { conn.close(); } catch(Exception e) {}
			}
			return result;
		}	
}